/************************************************************************************/
/*  Program name CAPFAIMP_TXN_HEADERS.sql                                  			*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 16 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER FA_CUA_TRANSACTION_HEADERS_BRI DISABLE;
ALTER TRIGGER FA_CUA_TRX_HEADERS_ARI2 DISABLE;
ALTER TRIGGER FA_TRANSACTION_HEADERS_ARI DISABLE;
ALTER TRIGGER FA_TRANSACTION_HEADERS_BRI DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_TXN_HEADERS';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_TXN_HEADERS;
	
	lv_Next_Seq_ID	NUMBER;
	
BEGIN
	UPDATE
		FA_TRANSACTION_HEADERS
	SET	
		BOOK_TYPE_CODE	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_CODE,1,4), BOOK_TYPE_CODE)
	WHERE BOOK_TYPE_CODE IN
		(
		SELECT 
			BOOK_TYPE_CODE
		FROM 
			CRST_FA_TXN_HEADERS
		);
	
	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	FA_TRANSACTION_HEADERS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_FA_TXN_HEADERS_MAP VALUES
			(
			C1.TRANSACTION_HEADER_ID,   
			lv_Next_Seq_ID 
			);

			INSERT INTO FA_TRANSACTION_HEADERS
			VALUES
			(
			lv_Next_Seq_ID,
			C1.BOOK_TYPE_CODE,
			CAPBRIMP_MAPPING.GetAssetIDMapping(C1.ASSET_ID),
			C1.TRANSACTION_TYPE_CODE,
			C1.TRANSACTION_DATE_ENTERED,
			C1.DATE_EFFECTIVE,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			C1.TRANSACTION_NAME,
			CAPBRIMP_MAPPING.GetFAInvoiceTXNIDMapping(C1.INVOICE_TRANSACTION_ID),
			C1.SOURCE_TRANSACTION_HEADER_ID,
			C1.MASS_REFERENCE_ID,
			C1.LAST_UPDATE_LOGIN,
			C1.TRANSACTION_SUBTYPE,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE_CATEGORY_CODE,
			C1.TRANSACTION_KEY,
			C1.AMORTIZATION_START_DATE,
			C1.CALLING_INTERFACE,
			C1.MASS_TRANSACTION_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/

ALTER TRIGGER FA_CUA_TRANSACTION_HEADERS_BRI ENABLE;
ALTER TRIGGER FA_CUA_TRX_HEADERS_ARI2 ENABLE;
ALTER TRIGGER FA_TRANSACTION_HEADERS_ARI ENABLE;
ALTER TRIGGER FA_TRANSACTION_HEADERS_BRI ENABLE;